<template>
  <div class="customer-management">
    <!-- 页面标题 -->
    <div class="page-header">
      <h2 class="page-title">客户管理</h2>
      <p class="page-subtitle">管理客户信息和联系方式</p>
    </div>

    <!-- 搜索和操作区域 -->
    <div class="global-card operation-card">
      <el-row :gutter="20" align="middle">
        <el-col :span="8">
          <el-input
              v-model="searchText"
              placeholder="搜索客户ID/姓名/电话"
              clearable
              @clear="handleSearch"
          >
            <template #prefix>
              <el-icon class="search-icon"><Search /></el-icon>
            </template>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="handleSearch" class="action-button">
            <el-icon><Search /></el-icon> 查询
          </el-button>
        </el-col>
        <el-col :span="4" :offset="8">
          <el-button
              type="success"
              @click="addDialogVisible = true"
              class="action-button new-customer-button"
          >
            <el-icon><Plus /></el-icon> 新增客户
          </el-button>
        </el-col>
      </el-row>
    </div>

    <!-- 客户表格 -->
    <div class="global-card data-card">
      <el-table :data="pageData.records" style="width: 100%" stripe height="calc(100vh - 280px)">
        <el-table-column prop="cid" label="客户ID" width="120" align="center" />
        <el-table-column prop="cname" label="客户姓名" width="150" align="center">
          <template #default="{ row }">
            <el-tag type="info" class="customer-tag">{{ row.cname }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="caddress" label="客户地址" width="250" align="center" />
        <el-table-column prop="ctel" label="联系电话" width="180" align="center">
          <template #default="{ row }">
            <div class="contact-cell">
              <el-icon><Phone /></el-icon>
              <span>{{ row.ctel }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" fixed="right" align="center">
          <template #default="{ row }">
            <el-button
                size="small"
                @click="handleEdit(row)"
                icon="Edit"
                class="edit-button"
            >编辑</el-button>
            <el-button
                size="small"
                type="danger"
                @click="handleDelete(row)"
                icon="Delete"
                class="delete-button"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <div class="pagination-wrapper">
        <el-pagination
            v-model:current-page="pageData.current"
            v-model:page-size="pageData.size"
            :page-sizes="[10, 20, 30, 40]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageData.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            class="custom-pagination"
        />
      </div>
    </div>

    <!-- 删除确认对话框 -->
    <el-dialog
        v-model="deleteDialogVisible"
        title="确认删除客户"
        width="400px"
        class="delete-dialog"
    >
      <div class="dialog-content">
        <el-icon class="dialog-icon" color="#F56C6C"><WarningFilled /></el-icon>
        <p>确定要删除客户 <strong>{{ deleteObj.cname }}</strong> 吗？</p>
        <p class="warning-text">此操作不可恢复，请谨慎操作！</p>
      </div>
      <template #footer>
        <el-button @click="deleteDialogVisible = false" class="cancel-button">取消</el-button>
        <el-button type="danger" @click="confirmDelete" class="confirm-delete-button">确认删除</el-button>
      </template>
    </el-dialog>

    <!-- 新增客户对话框 -->
    <el-dialog v-model="addDialogVisible" title="新增客户" class="form-dialog">
      <div class="dialog-header">
        <el-icon><User /></el-icon>
        <span>添加新客户信息</span>
      </div>
      <el-form :model="addObj" label-width="100px" class="customer-form">
        <el-form-item label="客户姓名" required>
          <el-input v-model="addObj.cname" autocomplete="off" placeholder="请输入客户姓名" />
        </el-form-item>
        <el-form-item label="联系电话" required>
          <el-input v-model="addObj.ctel" autocomplete="off" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="客户地址">
          <el-input
              v-model="addObj.caddress"
              autocomplete="off"
              placeholder="请输入客户地址"
              type="textarea"
              :rows="3"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="handlerAddCancel" class="cancel-button">取消</el-button>
        <el-button type="primary" @click="handlerAddConfirm" class="confirm-button">确认新增</el-button>
      </template>
    </el-dialog>

    <!-- 编辑客户对话框 -->
    <el-dialog v-model="editDialogVisible" title="编辑客户" class="form-dialog">
      <div class="dialog-header">
        <el-icon><Edit /></el-icon>
        <span>编辑客户信息</span>
      </div>
      <el-form :model="editObj" label-width="100px" class="customer-form">
        <el-form-item label="客户ID">
          <el-input v-model="editObj.cid" disabled />
        </el-form-item>
        <el-form-item label="客户姓名" required>
          <el-input v-model="editObj.cname" autocomplete="off" placeholder="请输入客户姓名" />
        </el-form-item>
        <el-form-item label="联系电话" required>
          <el-input v-model="editObj.ctel" autocomplete="off" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="客户地址">
          <el-input
              v-model="editObj.caddress"
              autocomplete="off"
              placeholder="请输入客户地址"
              type="textarea"
              :rows="3"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="handlerEditCancel" class="cancel-button">取消</el-button>
        <el-button type="primary" @click="handlerEditConfirm" class="confirm-button">保存修改</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import api from '../api/customer.js';

// 页面数据
const pageData = ref({
  records: [],
  total: 0,
  size: 10,
  current: 1,
  pages: 1
});

// 搜索文本
const searchText = ref('');

// 对话框控制
const addDialogVisible = ref(false);
const editDialogVisible = ref(false);
const deleteDialogVisible = ref(false);

// 表单对象
const addObj = ref({
  cname: '',
  caddress: '',
  ctel: ''
});

const editObj = ref({
  cid: '',
  cname: '',
  caddress: '',
  ctel: ''
});

const deleteObj = ref({
  cid: '',
  cname: ''
});

// 生命周期钩子
onMounted(() => {
  getTableList();
});

// 获取表格数据

const getTableList = async () => {

  const params = {
    current: pageData.value.current,
    size: pageData.value.size,
    searchtext: searchText.value || '' // 保持参数名一致
  };

  try {
    const result = await api.getCustomerPage(params);
    pageData.value = result;
  } catch (error) {
    console.error("获取客户数据失败:", error);
  }
};

// 分页处理
const handleSizeChange = (size) => {
  pageData.value.size = size;
  pageData.value.current = 1;
  getTableList();
};

const handleCurrentChange = (current) => {
  pageData.value.current = current;
  getTableList();
};

// 搜索处理
const handleSearch = () => {
  pageData.value.current = 1;
  getTableList();
};

// 删除客户
const handleDelete = (row) => {
  deleteObj.value = {
    cid: row.cid,
    cname: row.cname
  };
  deleteDialogVisible.value = true;
};

const confirmDelete = async () => {
  try {
    await api.deleteCustomer(deleteObj.value.cid);
    getTableList();
    deleteDialogVisible.value = false;
  } catch (error) {
    console.error("删除客户失败:", error);
  }
};

// 编辑客户
const handleEdit = (row) => {
  editDialogVisible.value = true;
  editObj.value = {...row};
};

const handlerEditConfirm = async () => {
  try {
    const submitData = {
      ...editObj.value
    };

    await api.editCustomer(submitData);
    getTableList();
    editDialogVisible.value = false;
  } catch (error) {
    console.error("更新客户失败:", error);
  }
};

// 新增客户
const handlerAddConfirm = async () => {
  try {
    const submitData = {
      ...addObj.value
    };

    await api.addCustomer(submitData);
    getTableList();
    addDialogVisible.value = false;
    // 重置表单
    addObj.value = {
      cname: '',
      caddress: '',
      ctel: ''
    };
  } catch (error) {
    console.error("添加客户失败:", error);
  }
};

// 取消操作
const handlerAddCancel = () => {
  addDialogVisible.value = false;
  addObj.value = {
    cname: '',
    caddress: '',
    ctel: ''
  };
};

const handlerEditCancel = () => {
  editDialogVisible.value = false;
};
</script>

<style scoped>
.customer-management {
  padding: 20px;
  background-color: #f0f7ff; /* 改为与用户管理一致的浅蓝色背景 */
  min-height: calc(100vh - 60px);
}

/* 页面标题样式 - 调整为深蓝色系 */
.page-header {
  margin-bottom: 25px;
  padding-bottom: 15px;
  border-bottom: 1px solid #d1e5f9; /* 浅蓝色分隔线 */
}

.page-title {
  color: #1a3b6d; /* 深蓝色标题 */
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 8px;
}

.page-subtitle {
  color: #4a7bb9; /* 蓝色副标题 */
  font-size: 15px;
}

/* 卡片样式 - 与用户管理一致 */
.operation-card {
  margin-bottom: 20px;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  background-color: #ffffff;
}

.data-card {
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  background-color: #ffffff;
}

/* 按钮样式 - 统一为蓝色主题 */
.action-button {
  padding: 8px 15px;
  font-weight: 500;
  transition: all 0.3s;
  border-radius: 8px;
  font-size: 14px;
}

.action-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

/* 新增按钮使用蓝色渐变 */
.new-customer-button {
  background: linear-gradient(135deg, #1e88e5, #42a5f5);
  border: none;
  color: white;
}

.new-customer-button:hover {
  background: linear-gradient(135deg, #1976d2, #2196f3);
}

/* 编辑按钮使用蓝色系 */
.edit-button {
  background-color: #e3f2fd;
  border-color: #90caf9;
  color: #1565c0;
}

/* 删除按钮使用红色系 */
.delete-button {
  background-color: #ffebee;
  border-color: #ffcdd2;
  color: #1565c0;
}

/* 标签样式 - 改为蓝色系 */
.customer-tag {
  background-color: #e3f2fd;
  border-color: #90caf9;
  color: #1565c0;
  padding: 4px 10px;
  font-weight: 500;
}

/* 联系电话图标颜色 */
.contact-cell .el-icon {
  color: #1e88e5; /* 蓝色图标 */
  font-size: 16px;
}

/* 分页样式 - 与用户管理一致 */
.pagination-wrapper {
  padding: 20px;
  display: flex;
  justify-content: center;
  margin-top: 20px;
}

.custom-pagination {
  background: white;
  padding: 10px 15px;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

/* 对话框样式 - 统一为蓝色主题 */
.delete-dialog {
  border-radius: 10px;
  background: linear-gradient(to bottom, #ffffff, #f8fbff);
}

.dialog-content {
  text-align: center;
  padding: 20px;
}

.dialog-icon {
  font-size: 48px;
  margin-bottom: 15px;
}

.warning-text {
  color: #e74c3c;
  font-size: 14px;
  margin-top: 10px;
}

.form-dialog {
  border-radius: 12px;
  background: linear-gradient(to bottom, #ffffff, #f8fbff);
}

.dialog-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 1px solid #e1ecf7; /* 浅蓝色分隔线 */
}

.dialog-header .el-icon {
  font-size: 24px;
  margin-right: 10px;
  color: #1e88e5; /* 蓝色图标 */
}

.dialog-header span {
  font-size: 18px;
  font-weight: 600;
  color: #1a3b6d; /* 深蓝色文字 */
}

.customer-form {
  padding: 0 20px;
}

/* 按钮样式 - 与用户管理一致 */
.cancel-button {
  background-color: #f5f7fa;
  border-color: #e1ecf7;
  color: #5a7ea9;
  margin-right: 15px;
}

.confirm-button {
  background: linear-gradient(135deg, #1e88e5, #42a5f5);
  border: none;
  color: white;
}

.confirm-button:hover {
  background: linear-gradient(135deg, #1976d2, #2196f3);
}

.confirm-delete-button {
  background: linear-gradient(135deg, #e53935, #d32f2f);
  border: none;
  color: white;
}

.confirm-delete-button:hover {
  background: linear-gradient(135deg, #d32f2f, #c62828);
}

/* 搜索图标颜色 */
.search-icon {
  color: #1e88e5; /* 蓝色图标 */
  font-size: 18px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .operation-card .el-row .el-col {
    margin-bottom: 10px;
  }

  .operation-card .el-col:last-child {
    margin-bottom: 0;
  }

  .new-customer-button {
    width: 100%;
  }

  .el-col:first-child {
    width: 100%;
    margin-bottom: 15px;
  }

  .el-table {
    font-size: 12px;
  }

  .el-table-column {
    padding: 4px !important;
  }

  .el-table-column:nth-child(3) {
    display: none;
  }
}

@media (max-width: 480px) {
  .el-table-column:nth-child(1) {
    width: 80px;
  }

  .el-table-column:nth-child(4) {
    display: none;
  }
}
</style>